/*
 * Copyright (c) 2006, 2022, webrx.cn All rights reserved.
 *
 */
package cn.webrx.controller;

import cn.webrx.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>Project: smvc2022 - HomeController
 * <p>Powered by webrx On 2022-03-19 11:06:15
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
@RestController
public class HomeController {
    @RequestMapping("/")
    public String index(@RequestParam(name = "nn", required = false, defaultValue = "李四") String name, HttpServletRequest req) {
        System.out.println(name);
        System.out.println(req.getParameter("name"));
        System.out.println(req.getMethod());
        System.out.println(req.getRemoteAddr());
        return String.format("你的姓名是：%s,%s,你的请求方法是:%s", name, req.getParameter("nn"), req.getMethod());
    }

    @RequestMapping("/p1")
    @ResponseBody
    public String p1(@RequestParam(required = false, defaultValue = "0") int id, @RequestParam(required = false, defaultValue = "") String name, @RequestParam(required = false, defaultValue = "0.0") double money) {

        return String.format("你的姓名是：%s,%f,%d", name, money, id);
    }

    @RequestMapping("/p2")
    @ResponseBody
    public String p2(int id, String name, double money) {
        return String.format("你的姓名是：%s,%f,%d", name, money, id);
    }

    @RequestMapping("/p3")
    @ResponseBody
    public String p3(User user) {
        return String.format("你的姓名是：%s", user);
    }

    //p4?id=66&name=李四 输出的 {"id":55,"name":"张三"}
    @RequestMapping("/p4")
    @ResponseBody
    public User p4(User user) {
        return user;
    }

    @GetMapping(value = {"/p5/{aa}.html", "/p5"})
    @ResponseBody
    public User p5(@RequestParam(name = "name", defaultValue = "李强", required = false) String name, @PathVariable(value = "aa", required = false) int id) {
        System.out.println("hello world");
        System.out.println(id);
        System.out.println(name);
        User u = new User();
        u.setId(id);
        u.setName(name);
        return u;
    }

    @GetMapping(value = "/p6/{nn}/{aa}.html")
    @ResponseBody
    public User p6(@PathVariable(name = "nn") String name, @PathVariable(value = "aa") int id) {
        System.out.println("hello world");
        System.out.println(id);
        System.out.println(name);
        User u = new User();
        u.setId(id);
        u.setName(name);
        return u;
    }

    @GetMapping({"/p7", "p7.html"})
    @ResponseBody
    public Map<String, Object> p7(Integer id, String name, Double money) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", id);
        map.put("name", name);
        map.put("money", money);
        return map;
    }

    @GetMapping({"/p8", "p8.html", "/p8/{a}.html", "/p8/{a}/{b}.shtml"})
    @ResponseBody
    public Map<String, Object> p8(@PathVariable(name = "a", required = false) Integer id, @PathVariable(name = "b", required = false) String name, @PathVariable(name = "c", required = false) Double money) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", id);
        map.put("name", name);
        map.put("money", money);
        return map;
    }

    @RequestMapping("/p9")
    public String p9(HttpServletRequest req) {
        return req.getMethod();
    }

    @RequestMapping("/pp1")
    public User pp1(@RequestBody User user) {
        System.out.printf("user....");
        return user;
    }

    @RequestMapping("/pp2")
    public Map<String, Object> pp2(@RequestBody Map<String, Object> map,HttpServletRequest req) {
        System.out.printf("map....");
        System.out.println(map);
        //System.out.println(req.getReader().readLine());
        return map;
    }

    @RequestMapping("/pp3")
    public String pp3(HttpServletRequest req) throws IOException {
        System.out.printf("req json....");
        String js = req.getReader().readLine();
        System.out.println(js);
        return js;
    }

    @GetMapping("/tt")
    public LocalDateTime tt(){
        return LocalDateTime.now();
    }
}
